package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportProduct;
import cn.itcast.domain.cargo.ExportProductExample;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import utils.BeanMapUtils;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/export/")
public class PdfController extends BaseController {

 @Reference
 private ExportService exportService;

 @Reference
 private ExportProductService exportProductService;

 /**
  * 用户点击此链接，展示PDF的入门案列
  */
 @RequestMapping("/exportPdf")
 public void exportPdf(String id) throws Exception {
/*  //获取模板路径
  String path = session.getServletContext().getRealPath("/") + "/jasper/demo01.jasper";
  //加载模板，数据填充
  JasperPrint jasperPrint = JasperFillManager.fillReport(path, new HashMap<>(), new JREmptyDataSource());
  //PDF文件导入（预览）
  JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());*/

/**
 * map类型数据填充
 */
/*  //获取模板路径
  String path = session.getServletContext().getRealPath("/") + "/jasper/demo02.jasper";
  //加载模板，数据填充
  Map map = new HashMap();
  map.put("username", "高顺");
  map.put("age", 26);
  map.put("email", "gaoshun@163.cn");
  map.put("address", "西凉");
  JasperPrint jasperPrint = JasperFillManager.fillReport(path, map, new JREmptyDataSource());
  //PDF文件导入（预览）
  JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());*/

/**
 * 数据源类型（list集合）的数据填充
 */
/*  //获取模板路径
  String path = session.getServletContext().getRealPath("/") + "/jasper/demo03.jasper";
  //加载模板，数据填充
  List list = new ArrayList();
  for (int i = 0; i < 10; i++) {
   User user = new User();
   user.setUserName("用户" + (i + 1));
   user.setEmail("yonghu" + (i + 1) + "@163.cn");
   user.setDeptName("java部门" + (i + 1));
   user.setCompanyName("传智播客" + (i + 1));
   list.add(user);
  }
  JRDataSource dataSource = new JRBeanCollectionDataSource(list);
  JasperPrint jasperPrint = JasperFillManager.fillReport(path, new HashMap<>(), dataSource);
  //PDF文件导入（预览）
  JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());*/

/**
 * 出口报运单打印
 */
  //根据id查询报运单对象
  Export export = exportService.findById(id);
  //根据报运单id查询报运单商品
  ExportProductExample example = new ExportProductExample();
  ExportProductExample.Criteria criteria = example.createCriteria();
  criteria.andExportIdEqualTo(id);
  List<ExportProduct> list = exportProductService.findAll(example);

  for (ExportProduct ep : list) {
   System.out.println(ep.getProductNo());
  }
  //将报运单对象转化为map集合
  Map map = BeanMapUtils.beanToMap(export);
  //通过jasper加载模板，导出模板
  String path = session.getServletContext().getRealPath("/") + "/jasper/export.jasper";
  JasperPrint jasperPrint = JasperFillManager.fillReport(path, map, new JRBeanCollectionDataSource(list));
  //PDF文件导入
  JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
 }
}
